import 'package:app/common/app_theme.dart';
import 'package:app/common/colors.dart';
import 'package:app/components/nodata_page.dart';
import 'package:app/pages/daily_work_report_module/top_dialog_page.dart';
import 'package:app/router/app_pages.dart';
import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import 'my_mission_module/my_mission_model.dart';

class MyMissionPage extends StatefulWidget {
  MyMissionPage({Key? key, required this.currents}) : super(key: key) {}
  List? currents = [];

  @override
  State<MyMissionPage> createState() => _MyMissionPageState();
}

class _MyMissionPageState extends State<MyMissionPage> {
  bool isTopShow = false;

  String mission_type = '关联任务';

  var page = 1;

  var dataArr = [];

  List<MyTaskItemModel>? missionList;

  requestData({var mission_type}) async {
    var params = {
      'task_type': 'create',
      'page': page,
      'limit': '10',
    };
    var json = await DioManager()
        .kkRequest(Address.missionList, params: params, method: 'get');
    if (json["errcode"] == 0) {
      missionList = <MyTaskItemModel>[];
      json['data']["list"].forEach((v) {
        missionList!.add(new MyTaskItemModel.fromJson(v));
      });
    }

    if (page == 1) {
      dataArr.clear();
      dataArr.addAll(missionList!);
    } else {
      dataArr.addAll(missionList!);
    }
    setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    requestData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          onPressed: () {
            Get.back();
          },
          icon: Image.asset(
            'images/leftarr.png',
            color: Colors.white,
            width: 15,
            height: 15,
          ),
        ),
        title: Text('关联任务'),
        actions: [
          Visibility(
              visible: widget.currents!.isNotEmpty,
              child: TextButton(
                  onPressed: () {
                    List namess = [];
                    widget.currents!.forEach((element) {
                      namess.add(dataArr[element].name);
                      print("element==>>>${element}");
                      print("element==>>>${dataArr[element].name}");
                    });
                    Get.back(result: {
                      'data': 'refresh',
                      'namess': namess,
                      'positions': widget.currents
                    });
                  },
                  child: const Text(
                    '确认',
                    style: TextStyle(color: Colors.white),
                  )))
        ],
        backgroundColor: AppColor.themeColor,
      ),
      body: Container(
        child: Column(
          children: [
            Container(
              color: Colors.white,
              height: 35,
              child: Row(
                children: [
                  Container(
                    margin: EdgeInsets.only(left: 15),
                    child: Row(
                      children: [
                        GestureDetector(
                          child: Text(
                            '${mission_type}',
                            style: TextStyle(fontSize: 13),
                          ),
                          onTap: () async {
                            isTopShow = true;
                            setState(() {
                              print(isTopShow);
                            });
                            var a;
                            if (mission_type == '我发布的任务') {
                              a = 0;
                            }
                            if (mission_type == '我负责的任务') {
                              a = 1;
                            }
                            if (mission_type == '我参与的任务') {
                              a = 2;
                            }
                            var data = await Get.dialog(TopDialogPage(a),
                                barrierColor: Colors.transparent);
                            if (data != null) {
                              print(data);
                              isTopShow = data['isShow'];
                              mission_type = data['text'];
                              requestData(mission_type: data['mission_type']);
                            }
                            setState(() {});
                          },
                        ),
                        isTopShow == false
                            ? Image.asset(
                                'images/arrow-down.png',
                                width: 12,
                                height: 12,
                              )
                            : Image.asset(
                                'images/arrow-up.png',
                                width: 12,
                                height: 12,
                                color: AppColor.themeColor,
                              )
                      ],
                    ),
                  ),
                  Container(
                      decoration: BoxDecoration(
                          color: AppColor.bgColor,
                          borderRadius: BorderRadius.all(Radius.circular(13))),
                      width: Get.width - 120,
                      alignment: Alignment.centerLeft,
                      margin: EdgeInsets.only(top: 0, left: 10, right: 10),
                      padding: EdgeInsets.only(left: 15),
                      height: 25,
                      child: Row(
                        children: [
                          Icon(
                            Icons.search,
                            size: 20,
                            color: AppColor.smallTextColor,
                          ),
                          Container(
                            alignment: Alignment.center,
                            color: Colors.white,
                            width: Get.width - 200,
                            height: 30,
                            child: TextField(
                              onChanged: (values) {},
                              decoration: InputDecoration(
                                  isCollapsed: true,
                                  border: InputBorder.none,
                                  hintText: '请输入关键词',
                                  hintStyle: TextStyle(
                                      color: AppColor.hintColor, fontSize: 13)),
                              style: TextStyle(
                                  color: AppColor.smallTextColor, fontSize: 14),
                            ),
                          )
                        ],
                      )),
                ],
              ),
            ),
            Expanded(
                child: Container(
              padding: EdgeInsets.only(top: 5),
              color: AppColor.bgColor,
              child: dataArr.isEmpty
                  ? NoDataPage()
                  : EasyRefresh.custom(
                      // emptyWidget: Container(
                      //   child: Text('暂无数据'),
                      // ),
                      onRefresh: () async {},
                      slivers: [
                        SliverList(
                          delegate: _mySliverChildBuilderDelegate(),
                        ),
                      ],
                    ),
            )),
          ],
        ),
      ),
    );
  }

  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
      (BuildContext context, int index) {
        MyTaskItemModel _model = dataArr[index];
        return GestureDetector(
          onTap: () {
            print("index===>>>$index");
            if (widget.currents!.contains(index)) {
              setState(() {
                widget.currents!.remove(index);
              });
            } else {
              setState(() {
                widget.currents!.add(index);
              });
            }
          },
          child: Container(
            // margin: EdgeInsets.all(5),
            // height: 10,
            margin: EdgeInsets.only(top: 10),
            padding: EdgeInsets.all(10),
            color: Colors.white,
            child: Row(
              children: [
                CircleAvatar(
                    radius: 21, child: Image.asset('images/log_icon.png')),
                Container(
                  margin: const EdgeInsets.only(
                    left: 10,
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        margin: const EdgeInsets.only(top: 4),
                        color: Colors.white,
                        width: Get.width - 100,
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              '${_model.name}',
                              style: appThemeData.textTheme.bodyText1,
                            )
                          ],
                        ),
                      ),
                      const SizedBox(
                        height: 3,
                      ),
                      Text(
                        '${_model.planDateBegin!.length > 10 ? _model.planDateBegin!.substring(0, 10) : _model.planDateBegin}',
                        style: appThemeData.textTheme.subtitle1
                            ?.copyWith(fontSize: 11),
                      ),
                      const SizedBox(
                        height: 3,
                      ),
                    ],
                  ),
                ),
                Container(
                  margin: const EdgeInsets.all(
                    2,
                  ),
                  child: Image.asset(
                    widget.currents!.contains(index)
                        ? 'images/check_image.png'
                        : 'images/uncheck_image.png',
                    width: 24,
                    height: 24,
                  ),
                )
              ],
            ),
          ),
        );
      },
      childCount: dataArr.length,
    );
  }
}
